/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.action;

import net.cellobject.javacodeformatter.Formatter;
import net.cellobject.javacodeformatter.model.FormatterManager;
import net.cellobject.javacodeformatter.model.IFormatterItem;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;

public class FormatChangedFilesActionDelegate implements IObjectActionDelegate {
	private IWorkbenchPart targetPart;

	public FormatChangedFilesActionDelegate() {
		super();
	}

	public void setActivePart(final IAction action, final IWorkbenchPart targetPart) {
		this.targetPart = targetPart;
	}

	public void run(final IAction action) {
		final IFormatterItem[] items = FormatterManager.getManager().getFavorites();
		if (items.length > 0) {
		
			final IWorkbenchPage page = targetPart.getSite().getWorkbenchWindow().getActivePage();
			//Format files			
			Formatter.getTPFormatter().formatFiles(page, items);
			//Remove Formatter items
			FormatterManager.getManager().removeFavorites(items);
		}
	}

	public void selectionChanged(final IAction action, final ISelection selection) {
	}
}
